package au.org.uptick.mygwtpapp.server.rpc;

import au.org.uptick.mygwtpapp.shared.FieldVerifier;
import au.org.uptick.mygwtpapp.shared.rpc.SendTextToServer;
import au.org.uptick.mygwtpapp.shared.rpc.SendTextToServerResult;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public class SendTextToServerHandler implements ActionHandler<SendTextToServer, SendTextToServerResult> {

    private Provider<HttpServletRequest> requestProvider;
    private ServletContext servletContext;

    @Inject
    SendTextToServerHandler(ServletContext servletContext, Provider<HttpServletRequest> requestProvider) {
        this.servletContext = servletContext;
        this.requestProvider = requestProvider;
    }

    @Override
    public SendTextToServerResult execute(SendTextToServer action, ExecutionContext context) throws ActionException {

        String input = action.getTextToServer();

        if (!FieldVerifier.isValidName(input)) {
            throw new ActionException("Name must be at least 4 characters long");
        }

        String serverInfo = servletContext.getServerInfo();
        String userAgent = requestProvider.get().getHeader("User-Agent");
        return new SendTextToServerResult("Hello, " + input
                                          + "!<br><br>I am running " + serverInfo
                                          + ".<br><br>It looks like you are using:<br>" + userAgent);
    }

    @Override
    public Class<SendTextToServer> getActionType() {
        return SendTextToServer.class;
    }

    @Override
    public void undo(SendTextToServer action, SendTextToServerResult result, ExecutionContext context) throws ActionException {
    }
}
